package com.beiing.tianshuai.tianshuai.adapter;

import android.content.Context;
import android.graphics.Point;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.app.HttpRequestConstant;
import com.beiing.tianshuai.tianshuai.base.BaseRecyclerViewAdapter;
import com.beiing.tianshuai.tianshuai.entity.GroupDetailsBean;
import com.beiing.tianshuai.tianshuai.widget.CircleImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bingoogolapple.bgabanner.BGAOnNoDoubleClickListener;

/**
 * Created by Jinni on 2017/9/20 0020.
 */

public class GroupMemberAdapter extends BaseRecyclerViewAdapter<GroupDetailsBean.DataBean.NumberBean.UserBean> {

    private Context mContext;
    private String mStatus;
    private boolean isOwner;
    private OnClickListener mListener;

    public GroupMemberAdapter(Context context, String status, boolean isOwner) {
        this.mContext = context;
        this.mStatus = status;
        this.isOwner = isOwner;
    }

    public void setListener(OnClickListener listener) {
        mListener = listener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder;
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_group_details_members, parent, false);
        holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public int getItemCount() {
        if (mList != null) {
            return mList.size() + 2;
        } else {
            return 0;
        }
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof ViewHolder) {
            // 初始化头像的宽高
            WindowManager windowManager =
                    (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
            Display display = windowManager.getDefaultDisplay();
            Point point = new Point();
            display.getSize(point);
            int width = point.x / 5 - (10 * 6);
            int height = point.x / 5 - (10 * 6);
            RequestOptions options = new RequestOptions()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .placeholder(R.mipmap.default_avatar)
                    .centerInside()
                    .override(width, height);
            ViewGroup.LayoutParams params =
                    ((ViewHolder) holder).mUserAvatar.getLayoutParams();
            params.width = width;
            params.height = height;
            ((ViewHolder) holder).mUserAvatar.setLayoutParams(params);
            if (position < mList.size()) {
                // 初始化昵称的宽
                ViewGroup.LayoutParams nameParams =
                        ((ViewHolder) holder).mUserNickname.getLayoutParams();
                nameParams.width = width;
                ((ViewHolder) holder).mUserAvatar.setVisibility(View.VISIBLE);
                ((ViewHolder) holder).mUserNickname.setVisibility(View.VISIBLE);
                ((ViewHolder) holder).mUserNickname.setLayoutParams(nameParams);
                ((ViewHolder) holder).mUserNickname.setEllipsize(TextUtils.TruncateAt.MIDDLE);
                ((ViewHolder) holder).mUserNickname.setMaxLines(1);
                ((ViewHolder) holder).mUserNickname.setGravity(Gravity.CENTER);
                // 获取数据
                final GroupDetailsBean.DataBean.NumberBean.UserBean bean = mList.get(position);
                String avatar = bean.getPhoto();
                String nickname = bean.getName();
                // 填充数据
                ((ViewHolder) holder).mUserNickname.setText(nickname);
                Glide.with(mContext.getApplicationContext())
                        .load(HttpRequestConstant.USER_PIC_HEAD + avatar)
                        .apply(options)
                        .into(((ViewHolder) holder).mUserAvatar);
                ((ViewHolder) holder).mUserAvatar.setOnClickListener(new BGAOnNoDoubleClickListener() {
                    @Override
                    public void onNoDoubleClick(View v) {
                        mListener.onItemClickListener(holder.getAdapterPosition(), bean.getUid());
                    }
                });
            } else if (position == mList.size()) {
                if (!isOwner) {
                    switch (mStatus) {
                        case "0":
                            ((ViewHolder) holder).mUserAvatar.setVisibility(View.VISIBLE);
                            ((ViewHolder) holder).mUserNickname.setVisibility(View.VISIBLE);
                            break;
                        case "1":
                            ((ViewHolder) holder).mUserAvatar.setVisibility(View.GONE);
                            ((ViewHolder) holder).mUserNickname.setVisibility(View.GONE);
                            break;
                    }
                }
                // 添加成员按钮
                ((ViewHolder) holder).mUserNickname.setText("");
                Glide.with(mContext.getApplicationContext())
                        .load(R.drawable.icon_add_user)
                        .apply(options)
                        .into(((ViewHolder) holder).mUserAvatar);
                ((ViewHolder) holder).mUserAvatar.setOnClickListener(new BGAOnNoDoubleClickListener() {
                    @Override
                    public void onNoDoubleClick(View v) {
                        mListener.onAddUserClickListener();
                    }
                });
            } else if (position == mList.size() + 1) {
                if (!isOwner) {
                    ((ViewHolder) holder).mUserAvatar.setVisibility(View.GONE);
                    ((ViewHolder) holder).mUserNickname.setVisibility(View.GONE);
                    return;
                }
                // 删除成员按钮
                ((ViewHolder) holder).mUserNickname.setText("");
                Glide.with(mContext.getApplicationContext())
                        .load(R.drawable.icon_del_user)
                        .apply(options)
                        .into(((ViewHolder) holder).mUserAvatar);
                ((ViewHolder) holder).mUserAvatar.setOnClickListener(new BGAOnNoDoubleClickListener() {
                    @Override
                    public void onNoDoubleClick(View v) {
                        mListener.onDelUserClickListener();
                    }
                });
            }
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.user_avatar)
        CircleImageView mUserAvatar;
        @BindView(R.id.user_nickname)
        TextView mUserNickname;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    public interface OnClickListener {
        void onItemClickListener(int position, String uid);

        void onAddUserClickListener();

        void onDelUserClickListener();
    }
}